package org.petstore.web.servlet;

import com.alibaba.fastjson.JSON;
import org.petstore.domain.Account;
import org.petstore.domain.CartItem;
import org.petstore.service.CartService;
import org.petstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class updateItemCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String itemId=req.getParameter("itemId");
        int quantity = Integer.parseInt(req.getParameter("quantity"));
        Account account;
        HttpSession session = req.getSession();
        account = (Account) session.getAttribute("account");
        String username = account.getUsername();
        CartService cartService = new CartService();
        cartService.updateItemByItemIdAndQuantity(username, itemId, quantity);
        resp.setContentType("text/json");//以json形式输出
        PrintWriter out=resp.getWriter();
        Result result1=new Result();
        CartItem cartItem=cartService.getCartItemByUsernameAndItemId(username,itemId);
        result1.setCode(12);
        result1.setMsg(cartItem.getTotal().toString());
        String str = JSON.toJSONString(result1);
        out.print(str);
        out.flush();
        out.close();
    }
}
